package kr.hkit.summury_java.erp.serviceUI;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.summury_java.erp.dao.DaoInterface;
import kr.hkit.summury_java.erp.dto.CodeTitle;
import kr.hkit.summury_java.erp.dto.Department;
import kr.hkit.summury_java.erp.dto.Employee;
import javax.swing.JLabel;
import java.awt.Font;


public class PanelListUI<T> extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTable table;
	private DaoInterface<T> dao;
	private ArrayList<T> lists;
	private String[][] arLists;
	private String[] colNames = null;
	private JLabel lblAlarm;
	public PanelListUI() {
		initialize();
	}
	
	public void setDao(DaoInterface<T> dao) {
		this.dao = dao;
		loadData();
		tableCellAlginAndWidth();
	}	

	private void initialize() {
		setLayout(new BorderLayout(0, 15));
		
		lblAlarm = new JLabel("New label");
		lblAlarm.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlarm.setFont(new Font("맑은 고딕", Font.BOLD, 15));
		add(lblAlarm, BorderLayout.NORTH);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);

		table = new JTable();
		table.setAutoCreateRowSorter(true);
		scrollPane.setViewportView(table);
	}
	
	public void loadData(){
		T temp = null;
		try {	
			lists = dao.listItems();
			arLists = new String[lists.size()][3];		
			for(int i = 0 ; i < lists.size() ;  i++){
				temp = lists.get(i);	
				if(temp instanceof Employee) {
					Employee e = (Employee) temp;
					arLists[i]=e.toArray();
				}else if (temp instanceof Department){
					Department d = (Department) temp;
					arLists[i]=d.toArray();
				}else if (temp instanceof CodeTitle) {
					CodeTitle c = (CodeTitle) temp;
					arLists[i]=c.toArray();
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			String[] eNames = {"사원번호", "사원명", "직급", "직속상사", "급여", "부서"};
			String[] dNames = {"부서번호", "부서명", "부서위치(층)"};
			String[] cNames = {"직급번호", "직급명"};			
			if(temp instanceof Employee) {
				colNames = eNames;
				lblAlarm.setText("사원 목록");
			} else if (temp instanceof Department){
				colNames = dNames;
				lblAlarm.setText("부서 목록");
			} else if (temp instanceof CodeTitle) {
				colNames = cNames;
				lblAlarm.setText("직책 목록");
			}
			TableModel model = new DefaultTableModel(arLists, colNames);
			table.setModel(model);
		}
	}
	
	private void tableCellAlginAndWidth() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
				
		TableColumnModel cModel = table.getColumnModel();
		
		for(int i = 0; i < colNames.length ; i++) {
			cModel.getColumn(i).setPreferredWidth(70);
			cModel.getColumn(i).setCellRenderer(dtcrCenter);
		}
	}
}
